Skip to content

Fix PrimeCheck returning true for negative odd numbers - #2030

Open
JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-prime-check-negative
Open

JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-prime-check-negative

Conversation

@JayPokale

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Maths/PrimeCheck.js rejected only 0, 1 and even numbers. For a negative odd n the loop condition i * i <= n is false from the start, so PrimeCheck(-1), PrimeCheck(-3) and every other negative odd number returned true. The two special cases are replaced by n < 2.

I added a test for negative numbers (it fails on master). I also compared PrimeCheck with a trial-division reference for -3..299: after this change they agree. npx vitest run Maths/test/PrimeCheck.test.js and npx prettier --check pass.

Written with the help of Claude Code (Claude Sonnet 5.5).

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

PrimeCheck only rejected 0, 1 and even numbers, and for a negative odd n the
loop condition i * i <= n is false at once, so PrimeCheck(-3) and
PrimeCheck(-1) were true. Reject everything below 2.
@JayPokale
JayPokale requested a review from appgurueu as a code owner November 21, 2026 15:21
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.91%. Comparing base (5c39e87) to head (4f49005).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2030      +/-   ##
==========================================
- Coverage   85.91%   85.91%   -0.01%     
==========================================
  Files         379      379              
  Lines       19778    19777       -1     
  Branches     3016     3014       -2     
==========================================
- Hits        16993    16992       -1     
  Misses       2785     2785              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants